package com.yiren.manager.ctrl.department;

import java.beans.IntrospectionException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yiren.core.exception.MyException;
import com.yiren.core.exception.SwallowException;
import com.yiren.core.utils.HTTPResult;
import com.yiren.core.utils.JsonMapperTool;
import com.yiren.core.utils.UUIDGenerator;
import com.yiren.entity.YrDepartment;
import com.yiren.manager.ctrl.GenericController;
import com.yiren.manager.form.department.YrDepartmentForm;
import com.yiren.manager.service.iface.department.YrDepartmentService;
import com.yiren.manager.utils.ObjectUtils;

/**
 * 部门管理控制类
 * @author Administrator
 *
 */
@RequestMapping("department")
@Controller
public class YrDepartmentController extends GenericController<YrDepartmentForm, YrDepartment>{

	@Autowired
	private YrDepartmentService selfService;
	
	//保存实体
	@RequestMapping("/save")
	@ResponseBody
	public HTTPResult<YrDepartmentForm> save(String data) throws SwallowException, MyException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, IntrospectionException{
		HTTPResult<YrDepartmentForm> result = new HTTPResult<YrDepartmentForm>();
		JsonMapperTool tool = new JsonMapperTool(data);
		YrDepartmentForm form = tool.mapper("department", YrDepartmentForm.class);
		if(null==form){
			throwExp("param.error");
		}
		if(null==form.getDepartmentId()||form.getDepartmentId().isEmpty()){
			form.setDepartmentId(UUIDGenerator.getUUID());
			form.setCreateTime(new Date());
		}
		form.setUpdateTime(new Date());
		YrDepartment entity = ObjectUtils.objectToObject(form, YrDepartment.class);
		selfService.save(entity);
		
		return result;
	}
	
	
	/**获取所有记录*/
	@RequestMapping("findAll")
	@ResponseBody
	public HTTPResult<YrDepartmentForm> findAll() throws MyException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{
		HTTPResult<YrDepartmentForm> result = new HTTPResult<YrDepartmentForm>();
		List<YrDepartment> entityList = selfService.findAll();
		List<YrDepartmentForm> formList = new ArrayList<YrDepartmentForm>();
		for(YrDepartment entity : entityList){
			YrDepartmentForm form = ObjectUtils.objectToObject(entity, YrDepartmentForm.class);
			formList.add(form);
		}
		result.setData(formList);
		return result;
	}
	
	/**
	 * 批量删除
	 * @param data
	 * @return
	 * @throws SwallowException
	 * @throws MyException
	 */
	@RequestMapping("deleteBatch")
	@ResponseBody
	public HTTPResult<YrDepartmentForm> deleteBatch(String data) throws SwallowException, MyException{
		HTTPResult<YrDepartmentForm> result = new HTTPResult<YrDepartmentForm>();
		JsonMapperTool tool = new JsonMapperTool(data);
		String[] ids = tool.mapper("ids", String[].class);
		if(null==ids){
			throwExp("param.error");
		}
		for(String id : ids){
			selfService.delete(id);
		}
		return result;
	}
	
	
	@Override
	public YrDepartmentForm toForm(YrDepartment e) throws Exception {
		YrDepartmentForm form = ObjectUtils.objectToObject(e, YrDepartmentForm.class);
		return form;
	}

	@Override
	public YrDepartment toEntity(YrDepartmentForm f) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

}
